$(function () {
    queryAjaxActivityList(1);
    $('body').on('click','#btn1,#btn2',function () {
        //点击搜索查询从第一页开始查询
        queryAjaxActivityList(1);
    });
    initLayerDate("#startDate",true);

    $('body').on('click','.deleteActivity',function () {
        var obj = $(this);
        var  activityId =$(this).data('id');
        icModale(2,"确认要删除所选活动么？",3,function () {
            updateActivityStatusByActivityIds(activityId,obj,3);
        });
    });
    //批量删除
    $("body").on("click","#deleteBatch",function () {
        var obj = $("input[name='activityid']:checked");
        var activityids = obtainIds(obj);//获取勾选ids
        if(!$.isEmptyObject(activityids)){
            icModale(2,"确认要删除所选活动么？",3,function () {
                updateActivityStatusByActivityIds(activityids,obj,3);
            });
        }
    });
    //批量冻结
    $('body').on("click", "#unlockBatch", function () {
        var obj = $("input[name='activityid']:checked");
        var activityids = obtainIds(obj);//获取勾选ids
        if (!$.isEmptyObject(activityids)) {
            updateActivityStatusByActivityIds(activityids, obj, 2);
        }
    });
    //批量恢复
    $("body").on("click", "#historyBatch", function () {
        var obj = $("input[name='activityid']:checked");
        var activityids = obtainIds(obj);//获取勾选ids
        if (!$.isEmptyObject(activityids)) {
            updateActivityStatusByActivityIds(activityids, obj, 1);
        }
    });
    //单个冻结恢复
    $('body').on('click', '.unlockBatch', function () {
        var activityIds = $(this).data('id');
        var status = $(this).data('status');
        var obj = $(this);
        if (status == 1) {
            updateActivityStatusByActivityIds(activityIds, obj, 2);
        }
        if (status == 2) {
            updateActivityStatusByActivityIds(activityIds, obj, 1);
        }
    });
    //批量发布
    $("body").on("click", "#releasseBatch", function () {
        var obj = $("input[name='activityid']:checked");
        var activityIds = obtainIds(obj);//获取勾选ids
        if (!$.isEmptyObject(activityIds)) {
            updateActivityReleaseStatusByActivityIds(activityIds, obj, 1);
        }
    });
    //单个发布
    $('body').on('click', '.releaseAndUnRelease', function () {
        alert(0);
        var activityId = $(this).data('id');
        var obj = $(this);
        updateActivityReleaseStatusByActivityIds(activityId,obj,1);
    });
    //判断勾选ID是否为空
    function obtainIds(obj) {
        if (obj.size() == 0) {
            icModale(0,"请选择要批量操作的数据");
            return null;
        } else {
            var activityIds = "";
            obj.each(function (index, em) {
                activityIds += em.value + ",";
            })
            activityIds = activityIds.substr(0, activityIds.length - 1);
            return activityIds;
        }
    }
    activityLookInfo();
});


/**
 * 获取角色列表页面
 * @param pageNo 当前页码
 */
function queryAjaxActivityList(pageNo) {
    var params = $("#search-form").serializeJSON();
    params = JSON.stringify(params);
    var obj = {
        url: baseloaction + '/admin/op/activity/queryOpActivityListPage',//请求URL
        contextId: 'activity-list-context',//响应内容页面设置内容ID
        params: params,//请求参数
        pageNo: pageNo,//页码
        pageCall: queryAjaxActivityList, //分页回调加载数据(点击上下页时调用,该方法也在加载页面完成时调用)
        selectAllId: 'select-all-activity', //全选框ID
        callback: function () {//数据加载完成回调
            $("#totalNum").html("（共"+total+"个活动）");
            $("#activityCount").val(total);
        }
    };
    ajaxGetListData(obj)
}


/**
 * 批量封禁，批量恢复，删除
 * @param userIds
 * @param obj
 * @param isavalible
 */
function updateActivityStatusByActivityIds(activityIds, obj, status) {
    $.ajax({
        url: baseloaction + "/admin/op/activity/updateActivityStatusByIds",
        data: {"activityIds": activityIds, "status": status},
        type: "post",
        dataType: "json",
        success: function (result) {
            if (result.success) {
                if (2 == status) {
                    obj.parents('tr').find('.status').html('');
                    obj.parents('tr').find('.status').append('<span class="label label-danger">冻结</span>');
                    obj.parents('tr').find(".unlockBatch").html("");
                    obj.parents('tr').find(".unlockBatch").data('status', '2');
                    obj.parents('tr').find(".unlockBatch").append('<i class="fa fa-history"></i>');
                    obj.parents('tr').find(".unlockBatch").attr('data-original-title',"恢复")
                }
                if (1 == status) {
                    obj.parents('tr').find('.status').html('');
                    obj.parents('tr').find('.status').append('<span class="label label-info">正常</span>');
                    obj.parents('tr').find(".unlockBatch").html("");
                    obj.parents('tr').find(".unlockBatch").data('status', '1');
                    obj.parents('tr').find(".unlockBatch").append('<i class="fa fa-unlock-alt"></i>');
                    obj.parents('tr').find(".unlockBatch").attr('data-original-title',"冻结")
                }
                if(3 == status) {
                    obj.parents('tr').html('');
                    var activityCount = $("#activityCount").val();
                    $("#totalNum").html('（共' + (activityCount - obj.size()) + '个公告）');
                    $("#activityCount").val((activityCount - obj.size()));
                    if ((activityCount - obj.size()) == 0) {
                        $("#tbodyActivity").append(
                            ' <td colspan="8" class="text-center">'
                            + '<div class="noDataTipsBox">'
                            + '<p><span class="c-4e fs16">亲，还没有数据~~~</span></p>'
                            + '</div>'
                            + '</td>'
                        );
                    }
                }
            } else {
                icModale(0, result.message);
            }
        },
        error: function (error) {
            icModale(0, "系统繁忙,请稍后重试");
        }
    })
}

/**
 * 批量发布
 * @param userIds
 * @param obj
 * @param isavalible
 */
function updateActivityReleaseStatusByActivityIds(activityIds,obj,releaseStatus) {
    $.ajax({
        url: baseloaction + "/admin/op/activity/updateActivityReleaseStatusByIds",
        data: {"activityIds": activityIds,"releaseStatus": releaseStatus},
        type: "post",
        dataType: "json",
        success: function (result) {
            if (result.success) {
                obj.parents('tr').find('.releaseStatus').html('');
                obj.parents('tr').find('.releaseStatus').append('<span class="label label-info">待发布</span>');
                obj.parents('tr').find(".releaseAndUnRelease").html("");
                icModale(0, result.message);
            } else {
                icModale(0, result.message);
            }
        },
        error: function (error) {
            icModale(0, "系统繁忙,请稍后重试");
        }
    })
}

function activityLookInfo() {
    $('body').on('click', '.activityLookInfo', function () {
        var activityId =$(this).data('id');
        var index = parent.layer.open({
            type: 2,
            shadeClose: false,
            shade: [0.3, '#000000'],
            title: '<strong>活动详情</strong>',
            area: winArea,
            content: baseloaction + "/admin/op/activity/opActivityInfoById?activityId="+activityId,
            btn: ['关闭'],
            yes: function (index, layero) {
                parent.layer.close(index);
            }
        });
    })
}